﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenMetaverse;

namespace Trim
{
    public partial class LoggingInForm : Form
    {
        public LoggingInForm()
        {
            InitializeComponent();
        }

        private void Cancel()
        {
            if (Program.client.SL.Network.Connected)
                Program.client.SL.Network.Logout();
            Program.client.LoginFormInstance.Show();
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void LoggingInForm_Load(object sender, EventArgs e)
        {
            Program.client.SL.Network.OnLogin += new OpenMetaverse.NetworkManager.LoginCallback(Network_OnLogin);
            Program.client.SL.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected);
            Program.client.SL.Network.OnDisconnected += new NetworkManager.DisconnectedCallback(Network_OnDisconnected);

            //Program.client.SL.Settings.CONTINUOUS_AGENT_UPDATES = false;
            Program.client.SL.Settings.SEND_AGENT_UPDATES = false;
            Program.client.SL.Settings.ALWAYS_DECODE_OBJECTS = false;
            Program.client.SL.Settings.ALWAYS_REQUEST_OBJECTS = false;
            //Program.client.SL.Settings.DEBUG = false;
            Program.client.SL.Settings.ENABLE_SIMSTATS = false;
            Program.client.SL.Settings.FETCH_MISSING_INVENTORY = false;
            Program.client.SL.Settings.MULTIPLE_SIMS = false;
            Program.client.SL.Settings.OBJECT_TRACKING = false;
            Program.client.SL.Settings.PARCEL_TRACKING = false;
            Program.client.SL.Settings.SEND_AGENT_THROTTLE = true;
            Program.client.SL.Settings.STORE_LAND_PATCHES = false;
            Program.client.SL.Throttle.Asset = 0;
            Program.client.SL.Throttle.Cloud = 0;
            Program.client.SL.Throttle.Land = 0;
            Program.client.SL.Throttle.Task = 0;
            Program.client.SL.Throttle.Texture = 0;
            Program.client.SL.Throttle.Wind = 0;

            Program.client.SL.Network.BeginLogin(Program.client.LoginParams);
        }

        private void Network_OnLogin(LoginStatus login, string message)
        {
            if (login == LoginStatus.Failed)
            {
                MessageBox.Show(message, "Login failed");
                BeginInvoke((MethodInvoker)delegate { Cancel(); });
            }
        }

        private void Network_OnDisconnected(NetworkManager.DisconnectType reason, string message)
        {
            if (reason != NetworkManager.DisconnectType.ClientInitiated)
            {
                MessageBox.Show(message, "Disconnected");
            }
            if(this.IsHandleCreated)
                BeginInvoke((MethodInvoker)delegate { Cancel(); });
        }

        private void Network_OnConnected(object sender)
        {
            Program.client.SL.Throttle.Set();
            //Program.client.SL.Appearance.SetPreviousAppearance(false);

            Program.client.LoginFormInstance.Invoke((MethodInvoker)delegate
            {
                Program.client.FriendsListFormInstance = new FriendsListForm();
                Program.client.FriendsListFormInstance.Show();
            });
            BeginInvoke((MethodInvoker)delegate { Close(); });
        }
    }
}
